<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
   <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
   <meta name="description" content="M2M with Arduino and RasperryPi" />
   <meta name="keywords" content="M2M,Arduino,RasperryPi" />
   <meta name="author" content="Benny Saxen" />
   <link rel="stylesheet" type="text/css" href="m2m-arh.css" title="M2M-ARH" media="screen,projection" />
   <?php
include("m2mLib.php");

session_start(); 
$id      = $_SESSION['id'];
$hb      = $_SESSION['hb'];
$s_year  = $_SESSION['s_year'];
$s_month = $_SESSION['s_month'];
$s_day   = $_SESSION['s_day'];

if(!$s_day)
{
    $s_day    = date('d');
    $s_month  = date('m');
    $s_year   = date('Y');
}

echo("<title>M2M $id ARH</title><script src=\"processing-1.4.1.js\"></script></head>");

echo("
<STYLE type=text/css>
A:link {COLOR: blue;text-decoration:none;}
A:visited {COLOR: blue}
A:hover {COLOR: green}
BODY {COLOR: black} 
</STYLE>
");

getStatus($id);
$hb = $g_hb[$id];

$analog = array();

$temp     = $_GET['id'];
if($temp) $id = $temp;

$temp    = $_GET['log'];
if($temp) $log = $temp;

$temp     = $_GET['hb'];  // Heart Beat   millisec
if($temp)
  {
    $g_hb[$id] = $temp;
    $hb = $temp;
  }

$temp = $_POST['arh_steps'];
if($temp)
  {
    echo("stepper");
    $g_dir = $_POST['arh_dir'];
    $g_steps = $temp;
    stepper($id,$g_dir,$g_steps);
  }

$temp = $_POST['arh_id'];
if($temp)
  {
    echo("Id $temp<br>");
    $id = $temp;
  }

$temp = $_POST['arh_year'];
if($temp)
  {
    $s_year = $temp;
  }
$temp = $_POST['arh_month'];
if($temp)
  {
    $s_month = $temp;
  }
$temp = $_POST['arh_day'];
if($temp)
  {
    $s_day = $temp;
  }

$temp = $_POST['arh_hb'];
if($temp)
  {
    echo("Heartbeat $temp<br>");
    $hb = $temp;
    setHeartBeat($id,$hb);
  }


$action = $_GET['action'];

if($action == 'dw')
  {
    $pin    = $_GET['pin'];
    $value  = $_GET['value'];
    digitalWrite($id,$pin,$value);
  }
if($action == 'pm')
  {
    $command =  PINMODE; // pinMode
    $arg1     = $_GET['pin'];
    $arg2     = $_GET['value'];
    pinMode($id,$pin,$value);
  }

if($action == 'all_dig_low')
  {
    digitalWriteAll($id,LOW);
  }
if($action == 'all_dig_high')
  {
    digitalWriteAll($id,HIGH);
  }
if($action == 'all_pm_inp')
  {
    pinModeAll($id,INPUT);
  }
if($action == 'all_pm_out')
  {
    pinModeAll($id,OUTPUT);
  }
$_SESSION['id'] = $id;
$_SESSION['hb'] = $hb;
$_SESSION['s_year']  = $s_year;
$_SESSION['s_month'] = $s_month;
$_SESSION['s_day']   = $s_day;


//===================================================
// HTML
//===================================================

if(!$id)$id = 9999;

$arduino = $g_arduino[$id];

echo("<body>");
echo("<h2>M2M Unit $id</h2>");
//$temp = analogRead($id,1);echo("($temp)");
echo("
  <script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js\"></script>
  <script type=\"text/javascript\">
  function update()
  {
   $.get(\"std-ajax.php?id=$id\", {user: '1'}, function(data){ $(\"#getmytime\").html(data);});  
   setTimeout('update()', 1000);
  }
  $(document).ready(function(){update();});
  </script>

  </head>

  <body>
 ");

//echo date(DATE_RFC822);
echo("<div id=\"getmytime\">no data</div>");

echo("<a href=\"m2m-sp.php?id=$id\"> SP </a>");echo("<a href=\"alltemp.php\"> Saxen Temperature </a><br>");
echo("
<form method=\"post\" action=\"index.php\" >
<input type=\"text\" name=\"arh_id\" size=\"5\" value=\"$id\">
<input type=\"submit\" value=\"Select Arduino Id\" > 
</form>
");

echo("
<form method=\"post\" action=\"index.php?id=$id\" >
<input type=\"text\" name=\"arh_hb\" size=\"5\" value=\"$hb\">
<input type=\"submit\" value=\"Heartbeat\" > 
</form>
");

/*echo("
<form method=\"post\" action=\"index.php?id=$id\" >
Steps <input type=\"text\" name=\"arh_steps\" size=\"5\" value=\"$g_steps\">
Direction <input type=\"text\" name=\"arh_dir\" size=\"5\" value=\"$g_dir\">
<input type=\"submit\" value=\"Stepper\" > 

</form>
");


echo("<table border=1><tr><th>");
echo("digitalWrite</th><th><a href=\"index.php?id=$id&action=all_dig_low\">LOW</a></th>");
for ($ii=2;$ii<=13;$ii++)
  {
    echo("<th><a href=\"index.php?id=$id&action=dw&pin=$ii&value=0\">$ii</a></th> ");	
  }
echo("</tr><th>digitalWrite</th><th><a href=\"index.php?id=$id&action=all_dig_high\">HIGH</a></th>  ");
for ($ii=2;$ii<=13;$ii++)
  {
    echo("<th><a href=\"index.php?id=$id&action=dw&pin=$ii&value=1\">$ii</a></th> ");	
  }
echo("</tr><th>pinMode</th><th><a href=\"index.php?id=$id&action=all_pm_inp\">INPUT</a></th>   ");
for ($ii=2;$ii<=13;$ii++)
  {
    echo("<th><a href=\"index.php?id=$id&action=pm&pin=$ii&value=0\">$ii</a></th> ");	
  }
echo("</tr><th>pinMode</th><th><a href=\"index.php?id=$id&action=all_pm_out\">OUTPUT</a></th>   ");
for ($ii=2;$ii<=13;$ii++)
  {
    echo("<th><a href=\"index.php?id=$id&action=pm&pin=$ii&value=1\">$ii</a></th> ");	
  }
echo("</tr></table>");
*/
//createPDE_analogGraphNow($id,1,1,1,1,1,1);

//echo("<div style=\"margin-top:10px;\" id=upper1 align=left margin>");
//echo("<canvas data-processing-sources=\"analogGraphNow-$id.pde\"></canvas>");
//echo("</div>");



echo("
<form method=\"post\" action=\"index.php?id=$id\" >
<input type=\"text\" name=\"arh_year\" size=\"5\" value=\"$s_year\">
<input type=\"text\" name=\"arh_month\" size=\"5\" value=\"$s_month\">
<input type=\"text\" name=\"arh_day\" size=\"5\" value=\"$s_day\">
<input type=\"submit\" value=\"Date\" > 
</form>
");
//--------------------------------------------------------------------
$config[graphId] = "HeaterInOut";
$config[year1]=$s_year;$config[month1]=$s_month;$config[day1]=stepTime($s_day,0);
$config[year2]=$s_year;$config[month2]=$s_month;$config[day2]=stepTime($s_day,0);
$config[id1]   = 2222;                    $config[id2]   = 2222;
$config[g1]    = 9;                       $config[g2]    = 11;
$config[s1]    = 100;                     $config[s2]    = 100;
$config[text1] = "Out";            	  $config[text2] = "In";
$config[floor] = 20.0;                   
$config[roof]  = 50.0;
$config[gH]    = 400;                     
$config[gW]    = 800;
$config[hr]    = 5;
$pdeFile = createPDE_2by2($config);
echo("<div style=\"margin-top:10px;\" id=upper1 align=left margin>");
echo("<canvas data-processing-sources=\"$pdeFile\"></canvas>");
echo("</div>");
//--------------------------------------------------------------------
//--------------------------------------------------------------------
$config[graphId] = "Gas";
$config[year1]=$s_year;$config[month1]=$s_month;$config[day1]=$s_day;
$config[id1]   = 2222;               
$config[g1]    = 12;                 
$config[s1]    = 100;                 
$config[text1] = "Gas";     
$config[floor] = 20.0;                   
$config[roof]  = 60.0;
$config[gH]    = 400;                     
$config[gW]    = 800;
$config[hr]    = 5;
$config[period] = 1;
$pdeFile = createPDE_1by1($config); 
echo("<div style=\"margin-top:10px;\" id=upper1 align=left margin>");
echo("<canvas data-processing-sources=\"$pdeFile\"></canvas>");
echo("</div>");
//--------------------------------------------------------------------
//--------------------------------------------------------------------
$config[graphId] = "Bedroom";
$config[year1]=$s_year;$config[month1]=$s_month;$config[day1]=$s_day;
$config[id1]   = 2222;               
$config[g1]    = 10;                 
$config[s1]    = 100;                 
$config[text1] = "Bedroom";     
$config[floor] = 17.0;                   
$config[roof]  = 22.0;
$config[gH]    = 400;                     
$config[gW]    = 800;
$config[hr]    = 1;
$config[period] = 0;
$pdeFile = createPDE_1by1($config); 
echo("<div style=\"margin-top:10px;\" id=upper1 align=left margin>");
echo("<canvas data-processing-sources=\"$pdeFile\"></canvas>");
echo("</div>");
//--------------------------------------------------------------------
//--------------------------------------------------------------------
$config[graphId] = "HeaterInOutDiff";
$config[year1]=$s_year;$config[month1]=$s_month;$config[day1]=stepTime($s_day,0);
$config[year2]=$s_year;$config[month2]=$s_month;$config[day2]=stepTime($s_day,0);
$config[id1]   = 2222;                    $config[id2]   = 2222;
$config[g1]    = 11;                      $config[g2]    = 9;
$config[s1]    = 100;                     $config[s2]    = 100;
$config[text1] = "Out-In Energy Consumption";    $config[text2] = "void";
$config[floor] =  0.0;                   
$config[roof]  = 10.0;
$config[gH]    = 400;                     
$config[gW]    = 800;
$config[hr]    = 1;
$pdeFile = createPDE_2by2diff($config);
echo("<div style=\"margin-top:10px;\" id=upper1 align=left margin>");
echo("<canvas data-processing-sources=\"$pdeFile\"></canvas>");
echo("</div>");
//--------------------------------------------------------------------
//--------------------------------------------------------------------
$config[graphId] = "EnergyNeed";
$config[year1]=$s_year;$config[month1]=$s_month;$config[day1]=stepTime($s_day,0);
$config[year2]=$s_year;$config[month2]=$s_month;$config[day2]=stepTime($s_day,0);
$config[id1]   = 1111;                    $config[id2]   = 2222;
$config[g1]    = 9;                       $config[g2]    = 10;
$config[s1]    = 100;                     $config[s2]    = 100;
$config[text1] = "EnergyNeed";            $config[text2] = "void";
$config[floor] =  0.0;                   
$config[roof]  = 30.0;
$config[gH]    = 400;                     
$config[gW]    = 800;
$config[hr]    = 5;
$pdeFile = createPDE_2by2diff($config);
echo("<div style=\"margin-top:10px;\" id=upper1 align=left margin>");
echo("<canvas data-processing-sources=\"$pdeFile\"></canvas>");
echo("</div>");
//--------------------------------------------------------------------
//--------------------------------------------------------------------
$config[graphId] = "GasTemp";

$config[year1]=$s_year;$config[month1]=$s_month;$config[day1]=$s_day;
$config[year2]=$s_year;$config[month2]=$s_month;$config[day2]=$s_day;
$config[id1]   = 2222;                    $config[id2]   = 2222;
$config[g1]    = 12;                       $config[g2]    = 9;
$config[s1]    = 100;                     $config[s2]    = 100;
$config[text1] = "Gas";               $config[text2] = "Water=Out";
$config[floor] = 20.0;                   
$config[roof]  = 60.0;
$config[gH]    = 400;                     
$config[gW]    = 800;
$config[hr]    = 5;
$pdeFile = createPDE_2by2($config); // outdoor temp (1111,9), bedroom temp (2222,10)
echo("<div style=\"margin-top:10px;\" id=upper1 align=left margin>");
echo("<canvas data-processing-sources=\"$pdeFile\"></canvas>");
echo("</div>");
//--------------------------------------------------------------------
//--------------------------------------------------------------------
$config[graphId] = "IndoorDetailed";
$config[year1]=$s_year;$config[month1]=$s_month;$config[day1]=$s_day;
$config[year2]=$s_year;$config[month2]=$s_month;$config[day2]=$s_day;
$config[id1]   = 1111;                    $config[id2]   = 2222;
$config[g1]    = 9;                       $config[g2]    = 10;
$config[s1]    = 100;                     $config[s2]    = 100;
$config[text1] = "Outdoor";               $config[text2] = "Bedroom";
$config[floor] = 19.0;                   
$config[roof]  = 22.0;
$config[gH]    = 400;                     
$config[gW]    = 800;
$config[hr]    = 1;
$pdeFile = createPDE_2by2($config); // outdoor temp (1111,9), bedroom temp (2222,10)
echo("<div style=\"margin-top:10px;\" id=upper1 align=left margin>");
echo("<canvas data-processing-sources=\"$pdeFile\"></canvas>");
echo("</div>");
//--------------------------------------------------------------------
//--------------------------------------------------------------------
$config[graphId] = "IndoorOutdoor";
$config[year1]=$s_year;$config[month1]=$s_month;$config[day1]=$s_day;
$config[year2]=$s_year;$config[month2]=$s_month;$config[day2]=$s_day;
$config[id1]   = 1111;                    $config[id2]   = 2222;
$config[g1]    = 9;                       $config[g2]    = 10;
$config[s1]    = 100;                     $config[s2]    = 100;
$config[text1] = "Outdoor";               $config[text2] = "Indoor";
$config[floor] = -20.0;                   
$config[roof]  = 30.0;
$config[gH]    = 300;                     
$config[gW]    = 800;
$config[hr]    = 5;
$pdeFile = createPDE_2by2($config); // outdoor temp (1111,9), bedroom temp (2222,10)
echo("<div style=\"margin-top:10px;\" id=upper1 align=left margin>");
echo("<canvas data-processing-sources=\"$pdeFile\"></canvas>");
echo("</div>");
//--------------------------------------------------------------------
/*createPDE_oneWire($id,$s_year,$s_month,$s_day,1,1,1,1,0);
echo("<div style=\"margin-top:10px;\" id=upper1 align=left margin>");
echo("<canvas data-processing-sources=\"w1Graph-$id.pde\"></canvas>");
echo("</div>");

createPDE_diffOneWire($id,$s_year,$s_month,$s_day,1,3);
echo("<div style=\"margin-top:10px;\" id=upper1 align=left margin>");
echo("<canvas data-processing-sources=\"w1Diff-$id.pde\"></canvas>");
echo("</div>");

createPDE_analog($id,$s_year,$s_month,$s_day,1,1,0,0,0,0);
echo("<div style=\"margin-top:10px;\" id=upper1 align=left margin>");
echo("<canvas data-processing-sources=\"analogGraph-$id.pde\"></canvas>");
echo("</div>");*/

echo("</body></html>"); 
?>
